package mailsystem.states;

import java.util.Set;

import mailsystem.Connection;
import mailsystem.Message;
import mailsystem.TelephoneResources;


public class MessageMenuStatus implements State {

	@Override
	public void runAction(Connection c, String key) {
		Message m = c.getCurrentMailbox().getCurrentMessage();
		if (key.equals("1")) {
			if (m == null)
			{
				c.speakAllPhones(TelephoneResources.NOMESSAGE, TelephoneResources.NOMESSAGE_AUDIO);
			}
			else
			{
				String output = "";
				
				if ( m.isAudio())
				{
					c.speakAllPhones(null, m.getText());
				}
				else
				{
					output += m.getText() + "\n";
					c.speakAllPhones(output, null);
				}
			}			
			c.showDisplayAllPhones(TelephoneResources.MESSAGE_MENU_TEXT);			
		} else if (key.equals("2")) {
			c.getCurrentMailbox().saveCurrentMessage();
			c.showDisplayAllPhones(TelephoneResources.MESSAGE_MENU_TEXT);
		} else if (key.equals("3")) {
			c.getCurrentMailbox().removeCurrentMessage();
			c.showDisplayAllPhones(TelephoneResources.MESSAGE_MENU_TEXT);
		} else if (key.equals("4")) {			
			if ( m != null )
				c.getCurrentMailbox().broadCastCurrentMessage();
			else
				c.speakAllPhones(TelephoneResources.NOMESSAGE, TelephoneResources.NOMESSAGE_AUDIO);
			c.showDisplayAllPhones(TelephoneResources.MESSAGE_MENU_TEXT);
		} else if (key.equals("5")) {
					
			if ( m != null )
			{
				String message = TelephoneResources.RESEND_TEXT;
				
				Set<String> keys = c.getCurrentMailbox().getMailBoxList().keySet();
				for (String mbox : keys)
					message += "\n" +  mbox + ".- " + c.getCurrentMailbox().getMailBoxList().get(mbox);
				c.setState(Connection.RESEND);
				c.showDisplayAllPhones(message);
			}
			else
			{
				c.speakAllPhones(TelephoneResources.NOMESSAGE, TelephoneResources.NOMESSAGE_AUDIO);
				c.showDisplayAllPhones(TelephoneResources.MESSAGE_MENU_TEXT);
			}
			
		} else if (key.equals("6")) {
			c.setState(Connection.MAILBOXMENU);			
			c.showDisplayAllPhones(TelephoneResources.MAILBOX_MENU_TEXT);
		}
	}

}
